package com.zy.pro.shop.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.core.service.impl.ServiceSupport;
import com.zy.pro.security.dao.SecurityMapper;
import com.zy.pro.security.dao.ShopSecurityMapper;
import com.zy.pro.security.model.Security;
import com.zy.pro.security.model.ShopSecurity;
import com.zy.pro.security.web.form.SecuritySearchForm;
import com.zy.pro.security.web.form.ShopSecuritySearchForm;
import com.zy.pro.shop.dao.ShopMapper;
import com.zy.pro.shop.dao.ShopUserMapper;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.model.ShopUser;
import com.zy.pro.shop.service.ShopService;
import com.zy.utils.BigDecimalUtil;
import com.zy.utils.MethodUtil;
@Service
public class ShopServiceImpl extends ServiceSupport<Shop> implements ShopService {

	@Autowired
    private ShopMapper<Shop> mapper;
	
	@Autowired
    private ShopUserMapper<ShopUser> shopUserMapper;
	
	@Autowired
    private SecurityMapper<Security> securityMapper;
	
	@Autowired
    private ShopSecurityMapper<ShopSecurity> shopSecurityMapper;

	public ShopMapper<Shop> getMapper() {
		return mapper;
	}

	@Override
	public void saveShopAndShopUser(Shop shop) throws Exception {
		Date now = new Date();
		//店铺状态
		shop.setShopStatus(1);//正常使用
		shop.setShopCreateTime(now);
		//插入店铺信息
		mapper.insert(shop);
		//插入店家信息
		ShopUser shopUser = shop.getShopUser();
		//shopUser.setShopUserName(shop.getShopUser().getShopUserName());
		//店铺主键
		shopUser.setShopId(shop.getShopId());
		//初始化密码123456
		shopUser.setPassword(MethodUtil.MD5("123456"));
		shopUser.setShopUserCreateTime(now);
		shopUserMapper.insert(shopUser);
	}

	@Override
	public void updateStatusByPaypal(Shop shop,String amt) throws Exception {
		//添加保障金缴付记录
		ShopSecurity shopSecurity = new ShopSecurity();	
		List<Security> sList = securityMapper.findAll(new SecuritySearchForm());
		if(!sList.isEmpty()){
			//保障金主键
			shopSecurity.setSecurityId(sList.get(0).getSecurityId());
			//保障金信誉度
			shop.setShopMoneyCredibility(new BigDecimal(sList.get(0).getSecurityCredibility()));
		}
		//店铺主键
		shopSecurity.setShopId(shop.getShopId());
		//缴付时间
		shopSecurity.setShopSecurityMoneyTime(new Date());
		//缴付金额
		shopSecurity.setShopSecurityMoney(new BigDecimal(amt));
		shopSecurityMapper.insert(shopSecurity);
		//判断如果存在保障金信誉度,则对店铺的总信誉度做计算(保障金信誉度+评论信誉度)
		if(shop.getShopMoneyCredibility() != null){
			Shop s = mapper.selectById(shop.getShopId());
			shop.setShopAllCredibility(BigDecimalUtil.add(shop.getShopMoneyCredibility(), s.getShopCredibility()));
		}
		//保障金已缴状态
		shop.setShopBondStatus(2);
		//设置保障金总金额
		Shop sInfo = mapper.selectById(shop.getShopId());
		if(sInfo != null && sInfo.getShopAllMoney() != null){
			shop.setShopAllMoney(BigDecimalUtil.add(new BigDecimal(amt), sInfo.getShopAllMoney()));
		}else{
			shop.setShopAllMoney(new BigDecimal(amt));
		}
		//更新店铺信息
		mapper.updateBySelective(shop);
	}
		
	
}
